package com.jing21.learn.tests;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.junit.Test;

import java.net.URLEncoder;
import java.util.Optional;

/**
 * Created by 郑靖 on 2017/10/8.
 */
public class HtmlParse {

    private static final String BAIDU_URL = "https://www.baidu.com/s?ie=UTF-8&wd=";



    @Test
    public void simple() {
//https://www.baidu.com/s?ie=utf-8&f=3&rsv_bp=1&rsv_idx=1&tn=baidu&wd=%E5%AE%9E%E5%8A%9B%E8%87%B3%E4%B8%8A%E4%B8%BB
// %E4%B9%89%E6%95%99%E5%AE%A4%E5%B0%8F%E8%AF%B4&oq=%25E5%25AE%259E%25E5%258A%259B%25E8%2587%25B3%25E4%25B8%258A%25E4
// %25B8%25BB%25E4%25B9%2589%25E6%2595%2599%25E5%25AE%25A4%25E5%25B0%258F%25E8%25AF%25B4&rsv_pq=a25aa97600007a5a
// &rsv_t=7cac4RI%2B7nsg64rfCG%2FCiSai8CWCRrwNCelFWObQMeu4iJ6W9YzmyP64ZyA&rqlang=cn&rsv_enter=0&prefixsug=%25E5%25AE
// %259E%25E5%258A%259B%25E8%2587%25B3%25E4%25B8%258A%25E4%25B8%25BB%25E4%25B9%2589%25E6%2595%2599%25E5%25AE%25A4
// %25E5%25B0%258F%25E8%25AF%25B4&rsp=2
        try {
            String encode = URLEncoder.encode("实力主义教室 小说", "UTF-8");
            System.err.println("encode is :" + encode);
            System.err.println("Uri is :" + BAIDU_URL + encode);

            Document document = Jsoup.connect(BAIDU_URL + encode).get();

            Optional.ofNullable(document)
                    .map(doc -> doc.body())
                    .map(body->body.select("#content_left"))
                    .ifPresent(elements -> {
                        System.err.println("size:" + elements.size());
                        elements.stream()
                                .forEach(element -> {
                                    System.err.println("=======================");
                                    System.err.println(element.html());
                                    System.err.println("============");
                                });
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
